package com.nlp.visualization.service.tasks;

import com.nlp.visualization.core.seg.ISegmentService;
import com.nlp.visualization.core.seg.SegmentType;
import com.nlp.visualization.core.seg.filter.SegFilter;
import com.nlp.visualization.pojo.NLP.seg.SegmentEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.RecursiveAction;

import static com.nlp.visualization.common.CONSTANTS.TASK_MAX;

@Service
public class NormalSegTask extends RecursiveAction {

    Logger logger = LoggerFactory.getLogger(NormalSegTask.class);

    @Autowired
    ISegmentService segmentService;


    private static final int MAX = TASK_MAX;

    private int start;
    private int end;
    private List<String> lines;
    private ConcurrentLinkedQueue<SegmentEntity.Results> queue;
    private SegmentType method;
    private SegFilter filter;

    public NormalSegTask(int start, int end, List lines, ConcurrentLinkedQueue<SegmentEntity.Results> queue, SegmentType method, SegFilter filter) {
        this.start = start;
        this.end = end;
        this.lines = lines;
        this.queue = queue;
        this.method = method;
        this.filter = filter;

    }

    @Override
    protected void compute() {
        // 当end-start的值小于MAX时候，开始执行任务
        if ((end - start) < MAX) {
            for (int i = start; i < end; i++) {

                //声明变量
                int count = 0;
                String text = lines.get(i);
                SegmentEntity.Results results = new SegmentEntity.Results();

                //填充结果的头部
                results.setSentenceId(i);
                results.setText(text);
                queue.add(results);
                //执行分词
                List<String> termsList = segmentService.executeSeg(text, method, filter);
                //处理分词结果
                Iterator iterator = termsList.iterator();
                while (iterator.hasNext()) {
                    String term = (String) iterator.next();
                    if (term.trim().equals(""))
                        continue;
                    String[] terms = term.split("/");
                    String word = "";
                    String pos = "";
                    word = terms[0];
                    if (terms.length > 1)
                        pos = terms[1];
                    SegmentEntity.Results.Items itemsBean = new SegmentEntity.Results.Items();
                    //设置id
                    itemsBean.setId(count);
                    count++;
                    //设置字节长度
                    itemsBean.setByte_length(term.toString().getBytes().length);
                    //设置词汇的标准化表达，主要针对时间、数字单位，没有归一化表达的，此项为空串
                    itemsBean.setFormal("");
                    //设置词汇的字符串
                    itemsBean.setItem(word);
                    //设置pos词性
                    itemsBean.setPos(pos);
                    //最细粒度的分词
                    List<String> baselist = new ArrayList<>();
                    List<String> minTerms = segmentService.executeSeg(word, SegmentType.ANSJ_BASIC, filter);
                    for (String s : minTerms) {
                        baselist.add(s.toString().split("/")[0]);
                    }
                    itemsBean.setBasic_words(baselist);

                    //添加进数组中
                    results.getItems().add(itemsBean);
                }



            }
        } else {
            // 将大任务分解成两个小任务
            int middle = (start + end) / 2;
            NormalSegTask left = new NormalSegTask(start, middle, lines, queue, method, filter);
            NormalSegTask right = new NormalSegTask(middle, end, lines, queue, method, filter);
            // 并行执行两个小任务
            left.fork();
            right.fork();
        }
    }

}
